home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / eflibpt4.zip / DEMO / DATATYPE / ELEMENT1.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-18  |  3KB  |  61 lines

  1. { Borland Pascal Extended Function Library - EFLIB (C) Johan Larsson, 1996
  2.   Demonstration; polymorphic elements for abstract data types #1
  3.  
  4.   EFLIB IS PROTECTED BY THE COPYRIGHT LAW AND MAY NOT BE COPIED, SOLD OR
  5.   MANIPULATED. FOR MORE INFORMATION, SEE PROGRAM MANUAL! THIS DEMONSTRAT-
  6.   ION PROGRAM MAY FREELY BE USED AND DISTRIBUTED.                          }
  7.  
  8.  
  9. uses EFLIBDEF, EFLIBINI, EFLIBDAT;
  10.  
  11. var Element1, Element2,
  12.     Element3, Element4, SomeNode : LinkageObjectPointerType;
  13.     SomeString : string; Index : word;
  14.  
  15.  
  16. begin
  17.      { Create a sample generic element for doubly linked data structures }
  18.      SomeString := 'Some basic data for the first element';
  19.      Element1 := New (GenericLinkageObjectPointerType,
  20.                  Initialize (SomeString, SizeOf(SomeString)));
  21.  
  22.      { Create one more element ... }
  23.      SomeString := 'Even more data for another element';
  24.      Element2 := New (GenericLinkageObjectPointerType,
  25.                  Initialize (SomeString, SizeOf(SomeString)));
  26.  
  27.      { Create an epty element }
  28.      SomeString := '';
  29.      Element3 := New (GenericLinkageObjectPointerType,
  30.                  InitializeEmpty);
  31.  
  32.      { Duplicate the second element into a forth element }
  33.      Element4 := New (GenericLinkageObjectPointerType,
  34.                  InitializeDuplicate (Element2));
  35.  
  36.      if Element4^.IsAllocated then { Verify that we actually duplicated an element }
  37.         WriteLn ('Duplicated an element');
  38.  
  39.      { Put elements together in a simple structure of linked elements }
  40.      Element1^.AttachAfter (Element2); { Attach second node after first }
  41.      Element2^.AttachAfter (Element3); { ... and the third after the second }
  42.      Element2^.AttachBefore (Element4); { ... and the forth element before the second }
  43.  
  44.      { Now we've got a very simple linked data structure without any
  45.        implementation into an advanced ADT. }
  46.  
  47.      SomeNode := Element1; { Start with first node }
  48.      for Index := 1 to 4 do begin
  49.          { One node will not be showed, hence it isn't allocated! }
  50.          if SomeNode^.IsIntact then { Just check that all links are intact }
  51.             WriteLn (Index:0, ': ', String(SomeNode^.DataPointer(0)^));
  52.          SomeNode := SomeNode^.Successor; { Go to the successor }
  53.      end;
  54.  
  55.      WriteLn ('Three nodes should be displayed, since the last node is empty.');
  56.  
  57.      { When elements are disposed, they're automatically detached
  58.        from any linked structure }
  59.      Element1^.Free; Element2^.Free; Element4^.Free; Element3^.Free;
  60.      if GlobalDataError then WriteLn ('Error(s) reported!');
  61. end.